<?php
use think\Db;


/**
 * 自定义常量
 * */
define('TIME_SPAN',5);  //+-5 minutes Env::get('time_span')   PatrolTask.php task 任务生成时间预判断区间


// 过滤左右空格
function trimArray($arr){
    $brr=[];
    foreach($arr as $v){
        array_push($brr,trim($v));
    }
    return $brr;
}

function lass()
{
    dump( Db::getLastSql());
    die;
}
function lasd()
{
    dump( Db::getLastSql());
}

function dd($a = false, $b = '')
{
    if ($a)
        dump($a);
    else {
        if ($b)
            dump(input($b));
        else
            dump(input('request.'));
    }
}
function de($a = false, $b = '')
{
    if ($a)
        dump($a);
    else {
        if ($b)
            dump(input($b));
        else
            dump(input('request.'));
    }
    die;
}
function df($aa)
{
    if(is_string($aa)){
        echo '<br/>';
        var_dump($aa);
    }elseif(is_int($aa) || is_float($aa)){
        echo '<br/>';
        var_dump($aa);

    }else{
        echo '---start:--对象数组--<br/>';
        foreach ($aa as $k => $v) {
            echo $k;
            $vv = object2array($v);
            dump($vv);
        }
        echo '---end:--对象数组--<br/>';
    }
}
// 拆分input 拼成 where 条件 过滤/null/null/ 等无效路由
function wmap(){
    $a = input('');
    $p[0] =$a['page']?$a['page']*1:1;
    $p[1] =$a['size']?$a['size']*1:10;
    unset($a['page']);
    unset($a['size']);
    $w = [];
    foreach($a as $k=>$v){
        if(isset($v) && $v !='null')
            $w[$k]= $v;
    }
    return [$p,$w];
}

//对象转数组
function object2array(&$object)
{
    $object = json_decode(json_encode($object), true);
    return $object;
}

function setUrl2()
{
    $_url = $_SERVER["REQUEST_URI"];
    echo $_url; // /main.php?m=activity&s=admin_activity_product_list&id=4&page=2
    $_par = parse_url($_url); //以?分隔拆分成数组
//    var_dump($_par);
    /*    array (size=2)
      'path' => string '/main.php' (length=9)
      'query' => string 'm=activity&s=admin_activity_product_list&id=4&page=2'*/

    if (isset($_par['query'])) {
        parse_str($_par['query'], $_query);  //$_par['query']将以数组形式被存储到query中
//        var_dump($_query);
        /*        array (size=4)
          'm' => string 'activity' (length=8)
          's' => string 'admin_activity_product_list' (length=27)
          'id' => string '4' (length=1)
          'page' => string '2' (length=1)*/
        unset($_query['page']);
//        var_dump($_query);
        /*array (size=3)
          'm' => string 'activity' (length=8)
          's' => string 'admin_activity_product_list' (length=27)
          'id' => string '4' (length=1)*/
        $_url = $_par['path'] . '?' . http_build_query($_query);
    }
//string '/main.php?m=activity&s=admin_activity_product_list&id=4'

    return $_url;
}


/**
 * 快捷调试输出
 * @param $raw
 * @param bool $ret
 * @param bool $html
 * @param float $depth
 * @param float $expand
 * @author luanpeng
 */
/*if(!function_exists('dd')){
    function dd($raw,$go = 0,$ret = false, $html = true, $depth = 1e3, $expand = 1e3){
        dump_r($raw, $ret, $html, $depth, $expand);
        if($go == 0){
            die;
        }
    }
}
if(!function_exists('dump_r')){
    require_once '../vendor/leeoniya/dump-r/src/dump_r/Core.php';
    function dump_r($raw, $ret = false, $html = true, $depth = 1e3, $expand = 1e3) {
        return Core::dump_r($raw, $ret, $html, $depth, $expand);
    }
}*/
if (!function_exists('getRandom')) {
    /**
     * 获取指定长度的任意字符串或数组
     * @param $length
     * @param int $type 0任意串 1纯数字 2纯字母 3纯大写字母 4纯小写字母
     * @author luanpeng
     */
    function getRandom($length, $type = 0)
    {
        $chars = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
            'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
            't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
            'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
            'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
        switch ($type) {
            case  0:
                $startKey = 0;
                $endKey = 61;
                break;
            case 1:
                $startKey = 52;
                $endKey = 61;
                break;
            case 2:
                $startKey = 0;
                $endKey = 51;
                break;
            case 3:
                $startKey = 26;
                $endKey = 51;
                break;
            case 4:
                $startKey = 0;
                $endKey = 25;
                break;
            default:
                $startKey = 0;
                $endKey = 61;
        }
        $return = '';
        for ($i = 0; $i < $length; $i++) {
            $return .= $chars[mt_rand($startKey, $endKey)];
        }
        return $return;
    }
}
if (!function_exists('getDistance')) {
    /**
     * 根据经纬度获取距离
     * @param $lat1 纬度1
     * @param $lng1 经度1
     * @param $lat2 纬度2
     * @param $lng2 经度2
     * @return float
     */
    function getDistance($lat1, $lng1, $lat2, $lng2)
    {
        $earthRadius = 6367000; //approximate radius of earth in meters
        $lat1 = ($lat1 * pi()) / 180;
        $lng1 = ($lng1 * pi()) / 180;
        $lat2 = ($lat2 * pi()) / 180;
        $lng2 = ($lng2 * pi()) / 180;
        $calcLongitude = $lng2 - $lng1;
        $calcLatitude = $lat2 - $lat1;
        $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);
        $stepTwo = 2 * asin(min(1, sqrt($stepOne)));
        $calculatedDistance = $earthRadius * $stepTwo;
        return round($calculatedDistance);
    }
}
// 比较时间
function checkTimeStatus($time)
{
    $nowtime = date("Y-m-d H:i:s", time());
    if ($nowtime > $time)
        return 1;
    else
        return 0;
}

//查询order()
function orders($listtype = false)
{
//    return $listtype?' i_offertime desc,i_no desc  ':' i_no desc, i_offertime desc ';
    switch ($listtype) {
        case "1":
            $order = ' i_no asc, i_offertime asc ';//case1
            break;
        case "5":
            $order = ' i_offertime desc ,i_no desc ';
            break;
        case "6":
            $order = '  i_offertime asc ,i_no asc';
            break;
        default:
            $order = ' i_no desc, i_offertime desc ';

    }
    return $order;
//    return $listtype?' i_no asc, i_offertime asc ':' i_no desc, i_offertime desc ';
}

/** 获得当月的第一天: 2018-01-01 00:00:00  最后一天: 2018-01-31 23:59:59
 * @param $date
 * @param int $has 1 全时间
 * @return array getTheMonth(date("Y-m-d"));
 */
function getTheMonth($date, $has = 0)
{
    $ha1 = $has ? " 00:00:00" : '';
    $ha2 = $has ? " 23:59:59" : '';
    $firstday = date('Y-m-01' . $ha1, strtotime($date));
    $lastday = date('Y-m-d' . $ha2, strtotime("$firstday +1 month -1 day"));
    return array($firstday, $lastday);
}

/**区分订单颜色 */
function qfdd($data)
{
    $ii = 1;
    if (count($data)) {
        for ($i = 0; $i < count($data); $i++) {
            if (($i > 0 && $data[$i - 1]['i_no'] != $data[$i]['i_no'])) {
                $ii++;
            }
            $dat[$i] = $ii % 2;
        }
    } else {
        $dat = [];
    }
    return $dat;
}


/** 清空/删除 文件夹
 * @param $dirname 文件夹路径
 * @param bool $self 是否删除当前文件夹
 * @return bool
 */
function delDir($dirname, $self = false)
{
    if (!file_exists($dirname)) {
        return false;
    }
    if (is_file($dirname) || is_link($dirname)) {
        return unlink($dirname);
    }
    $dir = dir($dirname);
    if ($dir) {
        while (false !== $entry = $dir->read()) {
            if ($entry == '.' || $entry == '..') {
                continue;
            }
            delDir($dirname . '/' . $entry);
        }
    }
    $dir->close();
    $self && rmdir($dirname);
}
//为了增加对移动设备的支持，在  /application/common.php加入以下函数
function isMobile()
{
    if (isset ($_SERVER['HTTP_X_WAP_PROFILE']))
    {
        return true;
    }
    if (isset ($_SERVER['HTTP_VIA']))
    {
        return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
    }
    if (isset ($_SERVER['HTTP_USER_AGENT']))
    {
        $clientkeywords = array ('nokia',
            'sony',
            'ericsson',
            'mot',
            'samsung',
            'htc',
            'sgh',
            'lg',
            'sharp',
            'sie-',
            'philips',
            'panasonic',
            'alcatel',
            'lenovo',
            'iphone',
            'ipod',
            'blackberry',
            'meizu',
            'android',
            'netfront',
            'symbian',
            'ucweb',
            'windowsce',
            'palm',
            'operamini',
            'operamobi',
            'openwave',
            'nexusone',
            'cldc',
            'midp',
            'wap',
            'mobile'
        );
        if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT'])))
        {
            return true;
        }
    }
    if (isset ($_SERVER['HTTP_ACCEPT']))
    {
        if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html'))))
        {
            return true;
        }
    }
    return false;
}
